package de.hwrBerlin.dataMining.view.vImage;

import java.util.ArrayList;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import de.hwrBerlin.dataMining.view.vImage.figures.Figure;
import de.hwrBerlin.dataMining.view.vImage.figures.Line;
import de.hwrBerlin.dataMining.view.vImage.figures.Oval;
import de.hwrBerlin.dataMining.view.vImage.figures.Rectangle;
import de.hwrBerlin.dataMining.view.vImage.figures.Text;

public class VImage {
    
    ArrayList<Figure> figures = new ArrayList<Figure>();
    private Device _device = null;
    private int _width = 0;
    private int _height = 0;
    private Color _foreground = null;
    private Color _background = null;
    private int _lineWidth = 1;
    private String _fontName = "Arial";
    private int _fontSize = 5;
    private int _antialias = SWT.ON;
    
    
    
    public VImage(Device pDevice, int pWidth, int pHeight) {
        _device = pDevice;
        _width = pWidth;
        _height = pHeight;
    }
    
    
    
    public void paintImage(GC pGc) {
        pGc.setFont(new Font(null, _fontName, _fontSize, SWT.NONE));
        for (Figure figure : figures) {
            figure.paintImage(pGc);
        }
    }
    
    
    
    public Image toImage() {
        Image image = new Image(_device, _width, _height);
        GC gc = new GC(image);
        paintImage(gc);
        return image;
    }
    
    
    
    public void paintSVG(Document pDocument) {
        for (Figure figure : figures) {
            figure.paintSVG(pDocument);
        }
    }
    
    
    
    public Document toSVG() {
        Document document = DocumentHelper.createDocument();
        document.addDocType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd");
        
        Element root = document.addElement("svg", "http://www.w3.org/2000/svg");
        
        root.addAttribute("width", ""+_width);
        root.addAttribute("height", ""+_height);
        
        paintSVG(document);
        return document;
    }
    
    
    
    public void drawLine(int pX1, int pY1, int pX2, int pY2){
        Line line = new Line(pX1, pY1, pX2, pY2);
        line.setForeground(_foreground);
        line.setLineWidth(_lineWidth);
        line.setAntialias(_antialias);
        figures.add(line);
    }
    
    
    
    public void drawRectangle(int pX, int pY, int pWidth, int pHeight){
        Rectangle ractangle = new Rectangle(pX, pY, pWidth, pHeight);
        ractangle.setForeground(_foreground);
        ractangle.setBackground(_background);
        ractangle.setLineWidth(_lineWidth);
        ractangle.setAntialias(_antialias);
        figures.add(ractangle);
    }
    
    
    
    public void drawOval(int pX, int pY, int pWidth, int pHeight){
        Oval oval = new Oval(pX, pY, pWidth, pHeight);
        oval.setForeground(_foreground);
        oval.setBackground(_background);
        oval.setLineWidth(_lineWidth);
        oval.setAntialias(_antialias);
        figures.add(oval);
    }
    
    
    
    public void drawText(String pText, int pX, int pY){
        Text text = new Text(pText, pX, pY);
        text.setForeground(_foreground);
        text.setBackground(_background);
        text.setLineWidth(_lineWidth);
        text.setAntialias(_antialias);
        text.setFontName(_fontName);
        text.setFontSize(_fontSize);
        figures.add(text);
    }
    
    
    
    public void setForeground(Color pColor) {
        _foreground = pColor;
    }
    
    
    
    public Color getForeground() {
        return _foreground;
    }
    
    
    
    public void setBackground(Color pColor) {
        _background = pColor;
    }
    
    
    
    public Color getBackground() {
        return _background;
    }
    
    
    
    public void setLineWidth(int pWidth) {
        _lineWidth = pWidth;
    }
    
    
    
    public int getLineWidth() {
        return _lineWidth;
    }
    
    
    
    public void setFontName(String pFontName) {
        _fontName = pFontName;
    }
    
    
    
    public String getFontName() {
        return _fontName;
    }
    
    
    
    public void setFontSize(int pFontSize) {
        _fontSize = pFontSize;
    }
    
    
    
    public int getFontSize() {
        return _fontSize;
    }
    
    
    
    public void setAntialias(int pAntialias) {
        _antialias = pAntialias;
    }
    
    
    
    public int getAntialias() {
        return _antialias;
    }
    
}
